<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="可秒杀库存" class="padding-left: 30px" prop="seckillStock">
        <el-input v-model="dataForm.seckillStock" placeholder="可秒杀库存"></el-input>
      </el-form-item>
      <el-form-item label="秒杀总数量" prop="seckillTotal">
        <el-input v-model="dataForm.seckillTotal" placeholder="秒杀总数量"></el-input>
      </el-form-item>
      <el-form-item label="库存数量" prop="stock">
        <el-input v-model="dataForm.stock" placeholder="库存数量"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        skuId: 0,
        seckillStock: '',
        seckillTotal: '',
        stock: ''
      },
      dataRule: {
        seckillStock: [{ required: true, message: '可秒杀库存不能为空', trigger: 'blur' }],
        seckillTotal: [{ required: true, message: '秒杀总数量不能为空', trigger: 'blur' }],
        stock: [{ required: true, message: '库存数量不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.skuId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.skuId) {
          this.$http({
            url: this.$http.adornUrl(`/item/stock/info/${this.dataForm.skuId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.seckillStock = data.stock.seckillStock
              this.dataForm.seckillTotal = data.stock.seckillTotal
              this.dataForm.stock = data.stock.stock
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/item/stock/${!this.dataForm.skuId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              skuId: this.dataForm.skuId || undefined,
              seckillStock: this.dataForm.seckillStock,
              seckillTotal: this.dataForm.seckillTotal,
              stock: this.dataForm.stock
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
